package cn.qingyun.gis.modules.scenes.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.scenes.contant.IntentionTypeEnum;
import cn.qingyun.gis.modules.scenes.entity.*;
import cn.qingyun.gis.modules.scenes.feign.PoiWgtInvokeFeignService;
import cn.qingyun.gis.modules.scenes.request.BindChannelRequest;
import cn.qingyun.gis.modules.scenes.request.KwResultRequest;
import cn.qingyun.gis.modules.scenes.request.PoiKwRequest;
import cn.qingyun.gis.modules.scenes.request.QueryUserPanRequest;
import cn.qingyun.gis.modules.scenes.service.IPoiScenarioService;
import cn.qingyun.gis.modules.scenes.service.PoiWgtInvokeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LHJ
 * @description
 * @date 2024/10/25 15:24
 */
@Slf4j
@Service
public class PoiWgtInvokeServiceImpl implements PoiWgtInvokeService {

    @Resource
    private PoiWgtInvokeFeignService poiWgtInvokeFeignService;

    @Resource
    private IPoiScenarioService iPoiScenarioService;

    @Override
    public Boolean isBindChannel(BindChannelRequest request) {
        log.debug("查询用户是否关联泛渠道接口入参{}", request);
        QueryUserPanRequest requestToWgt = new QueryUserPanRequest();
        requestToWgt.setEmployeeNumber(request.getTelNum());
        requestToWgt.setLoginPhone(request.getKwBossTel());
        String result = poiWgtInvokeFeignService.queryUserPanChannel(requestToWgt);
        log.debug("查询用户是否关联泛渠道网格通接口返回{}", result);
        if (StringUtils.isEmpty(result)) {
            throw new CommonException("查询用户是否关联泛渠道接口失败！");
        }
        JSONObject parseObj = JSONUtil.parseObj(result);
        String dataInfo = parseObj.getStr("dataInfo");
        if (StringUtils.isEmpty(dataInfo)) {
            return false;
        }
        JSONObject object = JSONUtil.parseObj(dataInfo);
        Boolean flag = object.getBool("flag");
        return flag != null && flag;
    }

    @Async
    @Override
    public void sendKwResultToWgt(PoiKwRequest request) {
        log.debug("摸排结果推送给网格通入参{}", request);
        IntentionInfo intentionInfo = request.getIntentionInfo();
        if (intentionInfo != null && intentionInfo.getIntentions().size() > 0) {
            KwResultRequest kwResultRequest = new KwResultRequest();
            BeanUtils.copyProperties(request, kwResultRequest);
            //查询poi基础信息
            QueryWrapper<PoiScenario> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(PoiScenario::getId, request.getId()).eq(PoiScenario::getRegionCityCode, request.getRegionCityCode());
            PoiScenario poiScenario = iPoiScenarioService.getBaseMapper().selectOne(wrapper);
            if (poiScenario == null) {
                log.debug("未查询到{}基础数据", request.getId());
                return;
            }
            //拿业务号码
            KdDeviceInfo kdDeviceInfo = request.getKdDeviceInfo();
            String kdRelatedPhone = null;
            if (kdDeviceInfo != null) {
                kdRelatedPhone = kdDeviceInfo.getKdRelatedPhone();
            }
            List<IntentionEntity> intentions = request.getIntentionInfo().getIntentions();
            String finalKdRelatedPhone = kdRelatedPhone;
            ArrayList<KwResultRequest> list = new ArrayList<>();
            intentions.forEach(intentionEntity -> {
                KwResultRequest resp = new KwResultRequest();
                Integer intentionType = intentionEntity.getIntentionType();
                String intentionTypeName = IntentionTypeEnum.getValueByCode(intentionType);
                BeanUtils.copyProperties(intentionEntity, resp);
                resp.setIntentionTypeName(intentionTypeName);
                resp.setRemindDate(intentionEntity.getReminderTime());
                resp.setReminderFlag("0");
                resp.setSaleFlag("0");
                resp.setGridName(poiScenario.getGridName());
                resp.setUserId(request.getUserId());
                resp.setAddress(poiScenario.getAddress());
                resp.setPoiName(poiScenario.getPoiName());
                resp.setEmployeeMobile(request.getKwBossTel());
                resp.setHandleMobile(finalKdRelatedPhone);
                resp.setBusinessCode(request.getChainId());
                resp.setIntentionRemark(intentionEntity.getRemark());
                if (intentionEntity.getIntentionType().equals(IntentionTypeEnum.ZERO.code) && CollectionUtils.isNotEmpty(intentionInfo.getPhoneList())){
                    resp.setIntentionRemark(intentionInfo.getPhoneList().toString());
                }else if (intentionEntity.getIntentionType().equals(IntentionTypeEnum.FIVE.code) && CollectionUtils.isNotEmpty(intentionInfo.getOtherIntentionList())){
                    resp.setIntentionRemark(intentionInfo.getOtherIntentionList().toString());
                }
                list.add(resp);
            });
            if (CollectionUtils.isNotEmpty(list)){
                log.debug("向网格通发送商机入参{}",list);
                String result = poiWgtInvokeFeignService.sendBusinessToWgt(list);
                log.debug("向网格通发送商机返回结果{}",result);
            }
        }
    }
}
